package cn.hotel.service.impl;

import cn.hotel.entity.Order;
import cn.hotel.entity.User;
import cn.hotel.mapper.OrderMapper;
import cn.hotel.service.OrderService;
import cn.hotel.service.RoomTypeService;
import cn.hotel.service.UserService;
import cn.hotel.event.OrderCreateEvent;
import cn.hotel.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private UserService userService;

    @Resource
    private RoomTypeService roomTypeService;

    //用于发布创建事件
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Override
    public List<Order> getOrdersByUserId(String userId) {
        return  orderMapper.queryOrdersByUserId(userId);
    }

    @Override
    @Transactional
    public int addOrder(Order order) {
        orderMapper.addOrder(order);
        System.out.println("=====orderId=="+order.getOrderId());
        roomTypeService.updateRoomRest(order.getRoomTypeId(),"-1");

        // 发布订单创建事件
        OrderCreateEvent event = new OrderCreateEvent(this, order);
        eventPublisher.publishEvent(event);
        return 1;
    }

    @Override
    public Order getOrderById(Integer orderId) {
        return  orderMapper.queryOrderById(orderId);
    }

    @Override
    public int getOrders() {
        return orderMapper.getOrders();
    }

    @Override
    public Order getOrderById(String orderId) {
        return orderMapper.queryOrderById(orderId);
    }

    @Override
    public int updateOrderStatus(String orderId,String status) {
        return orderMapper.setOrderStatus(orderId,status);
    }

    @Override
    public int payOrder(String userId,String password,String orderId) {
        User user = userService.queryUserById(userId);
        if (user==null||password==null){
            return 0;
        }

        //判断密码是否相等
        password = MD5Utils.MD5Encode(password);
        if (!password.equals(user.getPassword())){
            return 0;
        }

        //密码匹配成功后，对订单状态进行更新.1代表待入住
        this.updateOrderStatus(orderId,"1");
        return 1;
    }

    @Override
    @Transactional
    public int cancelOrder(Integer orderId) {
        Order order = orderMapper.queryOrderById(orderId);
        if (order==null){
            return 0;
        }

        orderMapper.deleteOrder(orderId);
        //删除订单后，将对应的房间类型的数据进行+1
        roomTypeService.updateRoomRest(order.getOrderTypeId(),"+1");
        return 1;
    }

    @Override
    public List<Order> allOrder(String params , Integer status) {
        List<Order> orders = orderMapper.allOrder(params , status);
        return orders;
    }

    @Override
    public int deleteByOrderId(Integer order) {
        int i = orderMapper.deleteByOrderId(order);
        return i;
    }

    @Override
    public int getOrderCount() {
        int orderCount = orderMapper.getOrderCount();
        return orderCount;
    }

    @Override
    public int updateOrder(Order order) {
        int i = orderMapper.updateOrder(order);
        return i;
    }

    @Override
    public List<Order> getOrderByName(String name) {
        List<Order> orderByName = orderMapper.getOrderByName(name);
        return orderByName;
    }

    @Override
    public Order getOrderByNameAndPhone(String name , String phone) {
        Order orderByNameAndPhone = orderMapper.getOrderByNameAndPhone(name ,phone);
        return orderByNameAndPhone;
    }
}
